//
//  FKSharePhotoBrower.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/17.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKSharePhotoBrower.h"
#import "FKSharePhotoCell.h"
static NSString *const ID = @"FKSharePhotoCell";

@interface FKSharePhotoBrower ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation FKSharePhotoBrower

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}

- (void)setImages:(NSArray *)images
{
    _images = images;
    
    NSMutableArray *datas = [NSMutableArray array];
    NSDictionary *userInfo = [FKCacheTool userInfo];
    for (NSInteger index = 0; index < images.count; index++) {
        NSMutableDictionary *data = [NSMutableDictionary dictionary];
//        data[@"indexStr"] = fkFORMAT(@"%zd/%zd", index + 1, images.count);
        data[@"url"] = images[index];
        if (self.url) {
            data[@"qrcode"] = fkFORMAT(@"%@&user_id=%@", self.url, userInfo[@"id"]);
        }
        if (self.saleTime) {
            data[@"saleTime"] = self.saleTime;
        }
        if (self.activeTime) {
            data[@"activeTime"] = self.activeTime;
        }
        data[@"name"] = userInfo[@"nickname"];
        data[@"avatar"] = userInfo[@"avatar"];
        data[@"level"] = userInfo[@"level"];
        [datas addObject:data];
    }
    self.datas = datas;
}

- (void)setUrl:(NSString *)url
{
    _url = url;
    
    if (self.datas.count > 0) {
        NSDictionary *userInfo = [FKCacheTool userInfo];
        for (NSMutableDictionary *data in self.datas) {
            data[@"qrcode"] = fkFORMAT(@"%@&user_id=%@", url, userInfo[@"id"]);
        }
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.backgroundColor = [UIColor blackColor];
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(18) textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter];
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemW = fkScreenW;
    CGFloat itemH = fkScreenH - fk_adjustW(80);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.pagingEnabled = true;
    collectionView.showsHorizontalScrollIndicator = false;
    [collectionView registerClass:[FKSharePhotoCell class] forCellWithReuseIdentifier:ID];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(44);
    }];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
//        make.top.equalTo(titleLabel.mas_bottom);
        make.height.offset(fkScreenH - fk_adjustW(80));
        make.centerY.offset(0);
    }];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKSharePhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.tag = indexPath.row;
    cell.data = self.datas[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismiss];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / fkScreenW;
    
    self.titleLabel.text = fkFORMAT(@"%zd/%zd", index + 1, self.datas.count);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.datas.count > 0) {
        [self.collectionView reloadData];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
            // 在layoutSubviews中调用这句话才会执行
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:false];
            
            self.titleLabel.text = fkFORMAT(@"%zd/%zd", self.currentIndex + 1, self.datas.count);
        });
    }
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
}

- (void)dismiss
{
    [self removeFromSuperview];
}

@end
